package com.aiti.lulian.utils;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.aiti.lulian.dto.DataBoardDto;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DateFilterUtil {

    private static final String DAY_BEFORE_YESTERDAY = "0";

    private static final String YESTERDAY = "1";

    private static final String TODAY = "2";

    private static final String THIS_WEEK = "3";

    private static final String LAST_WEEK = "4";

    private static final String THIS_MONTH = "5";

    private static final String DAY = "0";

    private static final String WEEK = "1";

    private static final String MONTH = "2";
    /**
     * 获取前天开始日期
     * @return
     */
    public static DateTime getBeginDayBeforeYesterday(){
        LocalDate todayLocalDate = LocalDate.now();
        LocalDate dayBeforeYesterday = todayLocalDate.minusDays(2);
        ZonedDateTime zonedDateTime = dayBeforeYesterday.atStartOfDay(ZoneId.systemDefault());
        java.time.Instant instant = zonedDateTime.toInstant();
        Date dayBeforeYesterdayDate = Date.from(instant);
        DateTime beginDayBeforeYesterday = DateUtil.beginOfDay(dayBeforeYesterdayDate);
        return beginDayBeforeYesterday;
    }
    /**
     * 获取前天结束日期
     * @return
     */
    public static DateTime getEndDayBeforeYesterday(){
        LocalDate todayLocalDate = LocalDate.now();
        LocalDate dayBeforeYesterday = todayLocalDate.minusDays(2);
        ZonedDateTime zonedDateTime = dayBeforeYesterday.atStartOfDay(ZoneId.systemDefault());
        java.time.Instant instant = zonedDateTime.toInstant();
        Date dayBeforeYesterdayDate = Date.from(instant);
        return DateUtil.endOfDay(dayBeforeYesterdayDate);
    }

    /**
     * 获取昨天开始日期
     * @return
     */
    public static DateTime getBeginYesterday(){
        return DateUtil.beginOfDay(DateUtil.yesterday());
    }

    /**
     * 获取昨天结束日期
     * @return
     */
    public static DateTime getEndYesterday(){
        return DateUtil.endOfDay(DateUtil.yesterday());
    }

    /**
     * 获取今天开始日期
     * @return
     */
    public static DateTime getBeginToday(){
        return DateUtil.beginOfDay(new Date());
    }

    /**
     * 获取今天结束日期
     * @return
     */
    public static DateTime getEndToday(){
        return DateUtil.endOfDay(new Date());
    }

    /**
     * 获取本周开始日期
     * @return
     */
    public static DateTime getBeginWeekDate(){
        return DateUtil.beginOfWeek(new Date());
    }
    /**
     * 获取本周结束日期
     * @return
     */
    public static DateTime getEndWeekDate(){
        return DateUtil.endOfWeek(new Date());
    }
    /**
     * 获取本月开始日期
     * @return
     */
    public static DateTime getBeginMonthDate(){
        return DateUtil.beginOfMonth(new Date());
    }

    /**
     * 获取本月结束日期
     * @return
     */
    public static DateTime getEndMonthDate(){
        return DateUtil.endOfMonth(new Date());
    }

    /**
     * 获取本月开始日期
     * @return
     */
    public static DateTime getLastBeginMonthDate(){
        DateTime dateTime = DateUtil.lastMonth();
        return DateUtil.beginOfMonth(dateTime);
    }

    /**
     * 获取本结束日期
     * @return
     */
    public static DateTime getLastEndMonthDate(){
        DateTime dateTime = DateUtil.lastMonth();
        return DateUtil.endOfMonth(dateTime);
    }

    /**
     * 获取本周开始日期
     * @return
     */
    public static DateTime getBeginLastWeekDate(){
        LocalDate todayLocalDate = LocalDate.now();
        LocalDate dayLastWeekLocalDate= todayLocalDate.minusDays(7);
        ZonedDateTime zonedDateTime = dayLastWeekLocalDate.atStartOfDay(ZoneId.systemDefault());
        java.time.Instant instant = zonedDateTime.toInstant();
        Date dayLastWeekDate = Date.from(instant);
        return DateUtil.beginOfWeek(dayLastWeekDate);
    }
    /**
     * 获取本周结束日期
     * @return
     */
    public static DateTime getEndLastWeekDate(){
        LocalDate todayLocalDate = LocalDate.now();
        LocalDate dayLastWeekLocalDate= todayLocalDate.minusDays(7);
        ZonedDateTime zonedDateTime = dayLastWeekLocalDate.atStartOfDay(ZoneId.systemDefault());
        java.time.Instant instant = zonedDateTime.toInstant();
        Date dayLastWeekDate = Date.from(instant);
        return DateUtil.endOfWeek(dayLastWeekDate);
    }
    /**
     * 根据筛选类型获取开始和结束日期
     * @param filteType
     * @return
     */
    public static Map<String, DateTime> getBeginAndEnd(String filteType){
        Map beginEndMap = new HashMap<String, DateTime>();
        if(DAY_BEFORE_YESTERDAY.equals(filteType)){
            beginEndMap.put("begin", getBeginDayBeforeYesterday());
            beginEndMap.put("end", getEndDayBeforeYesterday());
        }else if(YESTERDAY.equals(filteType)){
            beginEndMap.put("begin", getBeginYesterday());
            beginEndMap.put("end", getEndYesterday());
        }else if(TODAY.equals(filteType)){
            beginEndMap.put("begin", getBeginToday());
            beginEndMap.put("end", getEndToday());
        }else if(THIS_WEEK.equals(filteType)){
            beginEndMap.put("begin", getBeginWeekDate());
            beginEndMap.put("end", getEndWeekDate());
        }else if(LAST_WEEK.equals(filteType)){
            beginEndMap.put("begin", getBeginLastWeekDate());
            beginEndMap.put("end", getEndLastWeekDate());
        }else if(THIS_MONTH.equals(filteType)){
            beginEndMap.put("begin", getBeginMonthDate());
            beginEndMap.put("end", getEndMonthDate());
        }else{
            beginEndMap.put("begin", getBeginToday());
            beginEndMap.put("end", getEndToday());
        }
        return beginEndMap;
    }

    public static DataBoardDto getBeginAndEnd(String filteType, DataBoardDto dto){
        if(DAY.equals(filteType)){
            dto.setBeginDate(getBeginToday());
            dto.setEndDate(getEndToday());
            dto.setLastBeginDate(getBeginYesterday());
            dto.setLastEndDate(getEndYesterday());
        }else if(WEEK.equals(filteType)){
            dto.setBeginDate(getBeginWeekDate());
            dto.setEndDate(getEndWeekDate());
            dto.setLastBeginDate(getBeginLastWeekDate());
            dto.setLastEndDate(getEndLastWeekDate());
        }else if(MONTH.equals(filteType)){
            dto.setBeginDate(getBeginMonthDate());
            dto.setEndDate(getEndMonthDate());
            dto.setLastBeginDate(getLastBeginMonthDate());
            dto.setLastEndDate(getLastEndMonthDate());
        }
        return dto;
    }


}
